// pages/home/home.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    home: [],
    skip: 100,
    lishi: [],
    lisiLists: wx.getStorageSync('lishi') || [],
  },
  navToDetail(event) {
    // console.log(event.currentTarget.dataset);
    wx.navigateTo({
      url: `/pages/detail/detail?id=${event.currentTarget.dataset.id}&title=${event.currentTarget.dataset.title}`,
    })
    this.setData({
      lisiLists: [{
        id: `${event.currentTarget.dataset.id}`
      }, ...this.data.lisiLists]
    })
    var lishi = Array.from(new Set(this.data.lisiLists))
    wx.setStorageSync('lishi', lishi)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.showLoading({
      title: '加载中',
    })
    wx.request({
      url: 'http://cloud.agopage.com/6b83fd3bc06260ec/topPage?version=1.3.0',
      data: {
        skip: Math.floor(Math.random()*100 + 100)
      },
      header: {
        "content-type": 'application/json'
      },
      success: (res) => {
        this.setData({
            home: res.data.agopages
          })
          // wx.setStorageSync('lishiLists', this.data.lishiLists)
      },
      complete: () => {
        wx.hideLoading()
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },
  onRefresh:function(){
    //导航条加载动画
    wx.showNavigationBarLoading()
    //loading 提示框
    wx.showLoading({
      title: 'Loading...',
    })
    console.log("下拉刷新啦");
    setTimeout(function () {
      wx.hideLoading();
      wx.hideNavigationBarLoading();
      //停止下拉刷新
      wx.stopPullDownRefresh();
    }, 2000)
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.onRefresh();
    wx.request({
      url: 'http://cloud.agopage.com/6b83fd3bc06260ec/topPage?version=1.3.0',
      data: {
        skip: Math.floor(Math.random()*50 + 100)
      },
      header: {
        "content-type": 'application/json'
      },
      success: (res) => {
        this.setData({
            home: res.data.agopages
          })
          // wx.setStorageSync('lishiLists', this.data.lishiLists)
        }
      })
    },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    wx.showLoading({
      title: '加载中',
    })
    console.log('触底');
    wx.request({
      url: 'http://cloud.agopage.com/6b83fd3bc06260ec/topPage?version=1.3.0',
      header: {
        "content-type": 'application/json'
      },
      data: {
        skip: this.data.skip
      },
      success: (res) => {
        this.setData({
          home: [...this.data.home, ...res.data.agopages],
          skip: this.data.skip + 50
        })
      },
      complete: () => {
      wx.hideLoading()
    }
    })
    // console.log(this.data.skip);
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})